<?php

namespace App\Http\Controllers\Shop;

use App\Admin;
use App\Model\AddShop;
use App\Model\AddBrand;
use App\model\AddType;
use App\Model\ShopBrandModel;
use App\Model\AdminLogsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class BrandController extends Controller
{

	//品牌列表
	public function index(Request $request)
	{
		//搜索条件
		$keywords = $request->input('keywords');//关键字

		//品牌
		$list = AddBrand::where(function ($query) {
			if(request()->keywords){
				$query->where('name_cny',"like","%".request()->keywords."%");
				$query->Orwhere('name_en',"like","%".request()->keywords."%");
			}
		})->orderBy('id','desc')->paginate(15);
		$page = $request->input('page','');

		return view('shop.brand.index',compact('keywords','list','request','page'));
	}

	//添加页
	public function add()
	{
		return view('shop.brand.add');
	}

	//添加
	public function insert(Request $request){
		$post = $request->post();

		//事物
		DB::beginTransaction();
		try {
			if(AddBrand::where('name_cny',request()->name_cny)->first()){
				return ['code'=>0,'msg'=>"该品牌已存在"];
			}
			$type = New AddBrand();

			$type->name_cny = request()->name_cny;
			$type->name_en = request()->name_en;

			$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
			if (preg_match($auth_name, request()->name_cny)) {
				$pin = app('pinyin');
				$pinyin =$pin->abbr(request()->name_cny);
			}else{
				$pinyin = request()->name_cny;
			}
			//首字母
			$head = pinying($pinyin);
			$type->head = $head;
			$type->save();

			DB::commit();
		} catch (Exception $e) {
			DB::rollBack();
			throw $e;
			return returnJson(0,'操作失败,请稍后再试');
		}
		$msg = '【货源后台】添加品牌:'.request()->name_cny.'(id:'.$type->id.')';
		AdminLogsModel::addLogs($msg);
		return returnJson(1,'添加成功');
	}


	//删除
	public function del()
	{
		DB::beginTransaction();
		try{
			//品牌ID
			$id = request()->id;
			$list = AddType::where('id',$id)->first();
			if(AddShop::where('bid',$id)->first()){
				return returnJson(0, '该品牌分类下存在商品,不可删除');
			}else{
				AddBrand::where('id',$id)->delete();
				ShopBrandModel::where('bid',$id)->delete();
			}
			$msg = '【货源后台】删除品牌'.'(id:'.$id.')';
			AdminLogsModel::addLogs($msg);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return returnJson(0, '删除失败');
		}

		return returnJson(1, '删除成功');
	}

	//编辑
	public function edit(){
		$id = request()->id;
		$list = AddBrand::where('id',$id)->first();
		return view('shop.brand.edit',compact('id','list'));
	}

	//编辑
	public function update(){
		DB::beginTransaction();
		try{
			$list = AddBrand::where('id',request()->id)->first();

			$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
			if (preg_match($auth_name, request()->name_cny)) {
				$pin = app('pinyin');
				$pinyin =$pin->abbr(request()->name_cny);
			}else{
				$pinyin = request()->name_cny;
			}
			//首字母
			$head = pinying($pinyin);

			//更改品牌信息
			AddBrand::where('id', request()->id)->update([
				"name_cny" => request()->name_cny,
				"name_en"=>request()->name_en,
				"head" => $head,
			]);
			if($list['state'] == 1){
				//更改app后台信息
				ShopBrandModel::where('bid',request()->id)->update([
					'name' => request()->name_cny,
					'name_en' => request()->name_en,
					'head' => $head,
				]);
			}
			$msg = '【货源后台】编辑品牌:'.$list['name_cny'].'(id:'.request()->id.')';
			AdminLogsModel::addLogs($msg);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return returnJson(0, '更改失败');
		}
		return returnJson(1, '更改成功',$list['name_cny']);
	}

	//同步
	public function synchro(){
		$list = AddBrand::where('state',2)->get();
		if(count($list) <= 0){
			return ['code'=>0,'msg'=>'没有需要同步的数据!'];
		}

		DB::beginTransaction();
		try{
			foreach($list as $k=>$v){
				$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
				if (preg_match($auth_name, $v['name_cny'])) {
					$pin = app('pinyin');
					$pinyin =$pin->abbr($v['name_cny']);
				}else{
					$pinyin = $v['name_cny'];
				}
				//首字母
				$head = pinying($pinyin);

				ShopBrandModel::create([
					'name' => $v['name_cny'],
					'name_en' => $v['name_en'],
					'head' => $head,
					'bid' => $v['id'],
				]);
				AddBrand::where('id',$v['id'])->update(['state'=>1]);
			}
			DB::commit();
			$msg = '【货源后台】同步品牌';
			AdminLogsModel::addLogs($msg);
			return ['code'=>1,'msg'=>'已同步'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'同步失败'];
		}

	}


}
